package com.opencloud.ywt.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.utils.DateUtils;
import com.opencloud.ywt.client.model.entity.*;
import com.opencloud.ywt.provider.mapper.*;
import com.opencloud.ywt.provider.service.BhyfCityService;
import com.opencloud.ywt.provider.service.BhyfDzbpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 动量服务
 * @author yeyong
 */

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class BhyfDzbpServiceImpl implements BhyfDzbpService {
    @Autowired
    private BhyfDzbpFileMapper bhyfDzbpFileMapper;
    @Autowired
    private BhyfDzbpNoticeMapper bhyfDzbpNoticeMapper;
    @Autowired
    private BhyfDzbpTeacherMapper bhyfDzbpTeacherMapper;
    @Autowired
    private BhyfDzbpDutyMapper bhyfDzbpDutyMapper;
    @Autowired
    private BhyfDzbpMapMapper bhyfDzbpMapMapper;
    @Autowired
    private BhyfDzbpAreaMapper bhyfDzbpAreaMapper;

    @Override
    public List<BhyfDzbpFile> findFileListPage(PageParams pageParams) {
        BhyfDzbpFile query = pageParams.mapToObject(BhyfDzbpFile.class);
        QueryWrapper<BhyfDzbpFile> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getGid()), BhyfDzbpFile::getGid,query.getGid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getClassid()), BhyfDzbpFile::getClassid,query.getClassid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getUid()), BhyfDzbpFile::getUid,query.getUid());
        queryWrapper.lambda().orderByDesc(BhyfDzbpFile::getCreateTime);
        return bhyfDzbpFileMapper.selectList(queryWrapper);
    }

    @Override
    public List<BhyfDzbpNotice> findNoticeListPage(PageParams pageParams) {
        BhyfDzbpNotice query = pageParams.mapToObject(BhyfDzbpNotice.class);
        QueryWrapper<BhyfDzbpNotice> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getGid()), BhyfDzbpNotice::getGid,query.getGid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getClassid()), BhyfDzbpNotice::getClassid,query.getClassid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getUid()), BhyfDzbpNotice::getUid,query.getUid());
        queryWrapper.lambda().ge(BhyfDzbpNotice::getEnddate, DateUtils.formatDateTime(new Date()));
        queryWrapper.lambda().orderByDesc(BhyfDzbpNotice::getCreateTime);
        return bhyfDzbpNoticeMapper.selectList(queryWrapper);
    }

    @Override
    public List<BhyfDzbpTeacher> findTeacherListPage(PageParams pageParams) {
        BhyfDzbpTeacher query = pageParams.mapToObject(BhyfDzbpTeacher.class);
        QueryWrapper<BhyfDzbpTeacher> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getGid()), BhyfDzbpTeacher::getGid,query.getGid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getClassid()), BhyfDzbpTeacher::getClassid,query.getClassid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getUid()), BhyfDzbpTeacher::getUid,query.getUid());
        queryWrapper.lambda().orderByDesc(BhyfDzbpTeacher::getCreateTime);
        return bhyfDzbpTeacherMapper.selectList(queryWrapper);
    }

    @Override
    public List<BhyfDzbpDuty> findDutyListPage(PageParams pageParams) {
        BhyfDzbpDuty query = pageParams.mapToObject(BhyfDzbpDuty.class);
        QueryWrapper<BhyfDzbpDuty> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getGid()), BhyfDzbpDuty::getGid,query.getGid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getClassid()), BhyfDzbpDuty::getClassid,query.getClassid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getDutydate()), BhyfDzbpDuty::getDutydate,query.getDutydate());
        queryWrapper.lambda().orderByDesc(BhyfDzbpDuty::getCreateTime);
        return bhyfDzbpDutyMapper.selectList(queryWrapper);
    }

    @Override
    public Boolean deleteNotice(int noticeid,int gid,int classid,int uid)
    {
        QueryWrapper<BhyfDzbpNotice> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(noticeid), BhyfDzbpNotice::getId,noticeid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(gid), BhyfDzbpNotice::getGid,gid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(classid), BhyfDzbpNotice::getClassid,classid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(uid), BhyfDzbpNotice::getUid,uid);
        List<BhyfDzbpNotice> list = bhyfDzbpNoticeMapper.selectList(queryWrapper);
        if(list!=null && list.size()>0){
            bhyfDzbpNoticeMapper.deleteById(noticeid);
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Boolean deleteFile(String filekey,int gid,int classid,int uid)
    {
        QueryWrapper<BhyfDzbpFile> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(filekey), BhyfDzbpFile::getFilekey,filekey);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(gid), BhyfDzbpFile::getGid,gid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(classid), BhyfDzbpFile::getClassid,classid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(uid), BhyfDzbpFile::getUid,uid);
        List<BhyfDzbpFile> list = bhyfDzbpFileMapper.selectList(queryWrapper);
        if(list!=null && list.size()>0){
            bhyfDzbpFileMapper.deleteById(list.get(0).getId());
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Boolean deleteTeacher(int teacherid,int gid,int classid)
    {
        QueryWrapper<BhyfDzbpTeacher> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(teacherid), BhyfDzbpTeacher::getId,teacherid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(gid), BhyfDzbpTeacher::getGid,gid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(classid), BhyfDzbpTeacher::getClassid,classid);
        List<BhyfDzbpTeacher> list = bhyfDzbpTeacherMapper.selectList(queryWrapper);
        if(list!=null && list.size()>0){
            bhyfDzbpTeacherMapper.deleteById(teacherid);
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Boolean deleteDuty(int personid,String dutydate,int gid,int classid)
    {
        QueryWrapper<BhyfDzbpDuty> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(personid), BhyfDzbpDuty::getPersonid,personid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(gid), BhyfDzbpDuty::getGid,gid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(classid), BhyfDzbpDuty::getClassid,classid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(dutydate), BhyfDzbpDuty::getDutydate,dutydate);
        List<BhyfDzbpDuty> list = bhyfDzbpDutyMapper.selectList(queryWrapper);
        if(list!=null && list.size()>0){
            bhyfDzbpDutyMapper.deleteById(list.get(0).getId());
            return true;
        }else{
            return false;
        }
    }

    @Override
    public BhyfDzbpTeacher addTeacher(BhyfDzbpTeacher entiy)
    {
        bhyfDzbpTeacherMapper.insert(entiy);
        return  entiy;
    }


    @Override
    public BhyfDzbpNotice addNotice(BhyfDzbpNotice entiy)
    {
        bhyfDzbpNoticeMapper.insert(entiy);
        return  entiy;
    }


    @Override
    public BhyfDzbpDuty addDuty(BhyfDzbpDuty entiy)
    {
        bhyfDzbpDutyMapper.insert(entiy);
        return  entiy;
    }


    @Override
    public BhyfDzbpFile addFile(BhyfDzbpFile entiy)
    {
        bhyfDzbpFileMapper.insert(entiy);
        return  entiy;
    }

    @Override
    public BhyfDzbpTeacher updateTeacher(BhyfDzbpTeacher entiy)
    {
        bhyfDzbpTeacherMapper.updateById(entiy);
        return  entiy;
    }

    @Override
    public BhyfDzbpMap addMap(BhyfDzbpMap entiy)
    {
        bhyfDzbpMapMapper.insert(entiy);
        return  entiy;
    }

    @Override
    public BhyfDzbpArea addArea(BhyfDzbpArea entiy)
    {
        bhyfDzbpAreaMapper.insert(entiy);
        return  entiy;
    }

    @Override
    public BhyfDzbpArea updateArea(BhyfDzbpArea entiy)
    {
        bhyfDzbpAreaMapper.updateById(entiy);
        return  entiy;
    }

    @Override
    public List<BhyfDzbpArea> findAreaListPage(int gid) {
        QueryWrapper<BhyfDzbpArea> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(gid), BhyfDzbpArea::getGid,gid);
        queryWrapper.lambda().orderByDesc(BhyfDzbpArea::getCreateTime);
        return bhyfDzbpAreaMapper.selectList(queryWrapper);
    }

    @Override
    public List<BhyfDzbpMap> findGardenMap(int gid) {
        QueryWrapper<BhyfDzbpMap> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(gid), BhyfDzbpMap::getGid,gid);
        queryWrapper.lambda().orderByDesc(BhyfDzbpMap::getCreateTime);
        return bhyfDzbpMapMapper.selectList(queryWrapper);
    }

    @Override
    public Boolean deleteArea(int areaid,int gid)
    {
        QueryWrapper<BhyfDzbpArea> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(areaid), BhyfDzbpArea::getId,areaid);
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(gid), BhyfDzbpArea::getGid,gid);
        List<BhyfDzbpArea> list = bhyfDzbpAreaMapper.selectList(queryWrapper);
        if(list!=null && list.size()>0){
            bhyfDzbpAreaMapper.deleteById(areaid);
            return true;
        }else{
            return false;
        }
    }

    @Override
    public BhyfDzbpMap updateMap(BhyfDzbpMap entiy)
    {
        bhyfDzbpMapMapper.updateById(entiy);
        return  entiy;
    }
}